<!--
 * @Descripttion: 采购模板
 * @Author: xiao li
 * @Date: 2020-12-09 11:02:52
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-28 15:55:12
-->

<template>
    <div class="lb-goods-list">
        <top-nav />
        <div class="page-main">
          <lb-button size='medium' type='primary' opType='add' icon="el-icon-plus"  @click="$router.push('/malls/editPurchase')">{{$t('menu.PurchaseAdd')}}</lb-button>
          <div class="space-lg"></div>
          <el-row class="page-search-form">
            <el-form
            :inline="true"
            :model="searchForm"
            ref="searchForm"
            >
              <el-form-item label="模板名称" prop="title">
                <el-input v-model="searchForm.title" placeholder="请输入模板名称"></el-input>
              </el-form-item>
              <el-form-item>
                <lb-button size="medium" type="primary" icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
                <lb-button size="medium" icon="el-icon-refresh-left" style="margin-right:5px" @click="resetForm('searchForm')">{{$t('action.reset')}}</lb-button>
              </el-form-item>
            </el-form>
          </el-row>
          <el-table
            v-loading="loading"
            :data="tableData"
            :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange">
            <el-table-column
              prop="id"
              label="ID">
            </el-table-column>
            <el-table-column
              prop="title"
              width="170"
              label="模板名称">
            </el-table-column>
            <el-table-column
              prop="cover"
              label="封面图">
                <template slot-scope="scope">
                  <lb-image :src="scope.row.cover" />
                </template>
            </el-table-column>
            <el-table-column
              prop="top"
              label="排序值">
            </el-table-column>
            <el-table-column
              prop="status"
              label="是否上架">
                <template slot-scope="scope">
                  <el-switch
                    v-model="scope.row.status"
                    :active-value="1"
                    :inactive-value="0"
                    @change="updateItem(scope.row.id,scope.row.status)"
                    >
                  </el-switch>
                </template>
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="创建时间">
              <template slot-scope="scope">
                <div>{{scope.row.create_time | handleTime(1)}}</div>
                <div>{{scope.row.create_time | handleTime(2)}}</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              width="180px"
              label="操作">
                <template slot-scope="scope">
                  <div class="table-operate">
                    <lb-button size='mini' plain type='primary' @click="$router.push(`/malls/editPurchase?id=${scope.row.id}`)">{{$t('action.edit')}}</lb-button>
                    <lb-button size='mini' plain type='danger' @click="confirmDelGoods(scope.row.id)">{{$t('action.delete')}}</lb-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>
          <lb-page
          :batch="false"
          :page='searchForm.page'
          :pageSize='searchForm.limit'
          :total='total'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
          >
          </lb-page>

        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      searchForm: {
        title: '',
        cate_id: '',
        page: 1,
        limit: 10
      },
      cateOptions: [],
      tableData: [],
      total: 0
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    async submitFormInfo () {
      let {specsTable} = this
      let stock = []
      specsTable.map(item => {
        stock.push({id: item.true_id, stock: item.stock || 0})
      })
      let {code} = await this.$api.GoodsUpdateSpe({stock})
      if (code !== 200) return
      this.$message.success(this.$t('tips.successRev'))
      this.beforeClose()
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method 上下架菜品
     */
    updateItem (id, status) {
      this.$api.purchaseStatusUpdate({id, status}).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status !== -1) return
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - id.length) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - id.length) / this.searchForm.limit)
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    },
    /**
     * @method 获取菜品列表
     */
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.$api.purchaseList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    /**
     * @method: 删除
     * @param {*} id
     */
    confirmDelGoods (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      }).catch(() => {

      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
  .lb-goods-list{
    width: 100%;
    .page-main{
      width: 100%;
      padding: 20px;
      .shop-list-btn{
        .item-child{

        }
      }
      .el-table{
        img{
          width: 80px;
          height: 80px;
        }
      }
    }
  }
</style>
